// mircoApp/business-district/storeApply/storeApply.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
var app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    activeName: 0,
    dataList: [],
    titleArr: [],
    isLoadMore: true,

    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
  },

  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = "";
    const { url, ...otherParam } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== "") _Param += key + "=" + otherParam[key] + "&";
    });
    if (url === "") return;
    wx.navigateTo({
      url: url + "?" + _Param.slice(0, -1),
    });
  },
  init() {
    let _setdata = {};
    _setdata.dataList = [];
    _setdata.titleArr = [
      { id: "", name: "全部" },
      { id: 0, name: "待审核" },
      { id: 1, name: "审核通过" }
    ];
    _setdata.isLoadMore = true;

    this.setData(_setdata);
    this.setNewArr();
  },
  setNewArr() {
    this.data.titleArr.forEach((val, index) => {
      this.data.dataList.push({
        isLoadMore: true,
        loading: false,
        finished: false,
        page: 1,
        total_page: 0,
        list: []
      });
    });
    this.handleClick();
  },
  onClick(event){
    this.setData({
      activeName: event.detail.index
    });
    this.handleClick();
  },
  onSearch() {
    this.data.dataList[this.data.activeName].list = [];
    this.data.dataList[this.data.activeName].page = 1;
    this.handleClick();
  },
  async handleClick() {
    if (this.data.dataList[this.data.activeName].list && this.data.dataList[this.data.activeName].list.length > 0) {
      this.data.isLoadMore = this.data.dataList[this.data.activeName].isLoadMore;
      return;
    }
    let urlStr = app.getNetAddresss('plugin.business-district.frontend.controllers.promoter.store-apply');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        page: 1, 
        status: this.data.titleArr[this.data.activeName].id
      }
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);
    
    this.data.dataList[this.data.activeName].isLoadMore = true;
    this.data.dataList[this.data.activeName].page = 1;
    this.data.dataList[this.data.activeName].total_page = data.data.last_page;
    this.data.isLoadMore = true;
    this.setData({
      ['dataList[' + this.data.activeName + '].list']: data.data
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.dataList[this.data.activeName].isLoadMore = false;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.dataList[this.data.activeName].page >= this.data.dataList[this.data.activeName].total_page) {
      return this.setData({
        finished: true
      });
    } else {
      this.data.dataList[this.data.activeName].page = this.data.dataList[this.data.activeName].page + 1;
      let json = { page: this.data.dataList[this.data.activeName].page, status: this.data.titleArr[this.data.activeName].id };
      let index = this.data.activeName;
      let urlStr = app.getNetAddresss(this.data.dataList[this.data.activeName].url);
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resData) => {
          this.data.isLoadMore = true;
          this.data.dataList[index].isLoadMore = true;
          let res = resData.data;
          if (res.result !== 1) {
            this.data.dataList[index].page = this.data.dataList[index].page - 1;
            this.data.dataList[index].isLoadMore = false;
            this.data.isLoadMore = false;
            return app.tips(res.msg);
          }
          let myData = res.data.data;
          this.data.dataList[index].list = this.data.dataList[index].list.concat(myData); //数组拼接
          this.setData({
            ['dataList[' + index + '].list']: this.data.dataList[index].list,
          });
        },fail: (res) => {
          console.log(res);
        },
      });
    }
  },
  submitStauts(e) {
    let {status,id} = e.currentTarget.dataset;
    wx.showModal({
      content: `是否确认${status == 1 ? "通过" : "驳回"}该门店申请？`,
      complete: (res) => {
        if (res.confirm) {
          this.submitApply(status,id);
        }
      }
    })
  },
  async submitApply(status,id){
    this.isLock = true;
    if(!this.isLock) return;

    let urlStr = app.getNetAddresss('plugin.business-district.frontend.controllers.promoter.apply');
    let response = await app._postNetWork({
      url: urlStr,
      data: { apply: Number(status), apply_id: id }
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);
    this.isLock = false;
    this.onSearch();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage() {

  // }
})